﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ImageCtrl;

namespace OlympusUpload
{
    public partial class MainForm : Form
    {
        private ImageCtrl.ImageCtrl imageCtrl;
        public MainForm()
        {
            InitializeComponent();
            imageCtrl = new ImageCtrl.ImageCtrl();

            this.imageCtrl.Location = new Point(0,200);
            this.imageCtrl.Size = pbProcess.Size;
            this.imageCtrl.Height = 0;
            this.imageCtrl.TimeInterval = 100;
            this.Controls.Add(this.imageCtrl);
            this.imageCtrl.BringToFront();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.gif|*.gif|*.jpg|*.jpg";
            ofd.InitialDirectory = "\\";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbImage.Text = ofd.FileName;
            }
        }

        private void btRingBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.mp3|*.mp3|*.wav|*.wav";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbRing.Text = ofd.FileName;
            }
        }
        MobileUpload.MobileUploadService mus = new OlympusUpload.MobileUpload.MobileUploadService();
        MobileUpload.uploadMessage upmsg = new OlympusUpload.MobileUpload.uploadMessage();
        
        private void StartImage()
        {
            ImageCtrlItem image = new ImageCtrlItem();
            this.imageCtrl.Height = 20;
            image.ImageFile = "\\Process.gif";
            this.imageCtrl.Items.Add(image);
            this.imageCtrl.SelectIndex = 0;
        }

        private delegate void StopImageDelegate();

        private void StopImage()
        {
            if (this.imageCtrl.InvokeRequired)
            {
                StopImageDelegate simageDel = new StopImageDelegate(StopImage);
                this.Invoke(simageDel);
            }
            else
            {
                this.imageCtrl.Height = 0;
                this.imageCtrl.SelectIndex = -1;
                this.imageCtrl.Items.Clear();
            }
        }

        private void btUpload_Click(object sender, EventArgs e)
        {
            //Following to upload image
            string fileName = tbImage.Text;
            System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
            System.IO.FileStream fs = System.IO.File.OpenRead(fileName);
            byte[] fileByte = new byte[fi.Length];
            fs.Read(fileByte, 0, (int)fi.Length);
            upmsg.sender = "13811111111";
            upmsg.type = "0";
            upmsg.data = Convert.ToBase64String(fileByte);
            upmsg.dataLength = fi.Length.ToString();
            upmsg.fileType = fi.Extension.Substring(1);
            string[] str = new string[1];
            str[0] = "13800138000";
            upmsg.targetMsisdn = str;
            AsyncCallback asc = new AsyncCallback(AsyncCallBackMethod);
            StartImage();
            AddMessage("Start upload image");
            //mus.BeginuploadMessage(upmsg, asc, mus);
            try
            {
                string strReturn = mus.uploadMessage(upmsg);
                if (strReturn != null)
                    AddMessage(strReturn);
                else
                    AddMessage("Failed");

            }
            catch (Exception ex)
            {
                AddMessage(ex.Message);
            }
            //mUpload.uploadMessageAttach("13651214844", 0, Convert.ToBase64String(fileByte), (int)fi.Length, fi.Extension, null);
            
        }

        private delegate void AddMessageDelegate(string msg);

        private void AddMessage(string msg)
        {
            if (lbInfo.InvokeRequired)
            {
                AddMessageDelegate addMsg = new AddMessageDelegate(AddMessage);
                this.Invoke(addMsg, new object[] { msg });
            }
            else
            {
                lbInfo.Text += string.Format("{0}\r\n", msg);
            }
        }

        private void AsyncCallBackMethod(IAsyncResult iAResult)
        {
            if (iAResult.IsCompleted)
            {
                try
                {
                    MobileUpload.MobileUploadService mDel = (MobileUpload.MobileUploadService)iAResult.AsyncState;
                    if (mDel.EnduploadMessage(iAResult).ToLower() == "successed")
                    {
                        StopImage();
                        AddMessage("Upload success.");
                    }
                    else
                    {
                        StopImage();
                        AddMessage("Upload failed.");
                    }
                }
                catch (Exception)
                {
                    StopImage();
                    AddMessage("Upload failed.");
                }
            }

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            string fileName = tbRing.Text;
            System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
            System.IO.FileStream fs = System.IO.File.OpenRead(fileName);
            byte[] fileByte = new byte[fi.Length];
            fs.Read(fileByte, 0, (int)fi.Length);
            upmsg.sender = "13811111111";
            upmsg.type = "1";
            upmsg.data = Convert.ToBase64String(fileByte);
            upmsg.dataLength = fi.Length.ToString();
            upmsg.fileType = fi.Extension.Substring(1);
            string[] str = new string[1];
            str[0] = "13800138000";
            upmsg.targetMsisdn = str;
            AsyncCallback asc = new AsyncCallback(AsyncCallBackMethod);
            StartImage();
            AddMessage("Start upload ring");
            mus.BeginuploadMessage(upmsg, asc, null);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}